package com.polaris.live.common.util

import android.app.Activity
import android.content.Context
import android.provider.Settings
import android.view.View
import android.view.Window
import androidx.annotation.ColorRes
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.BarUtils
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.OSUtils
import com.polaris.live.common.R
import com.polaris.live.common.ext.dp

/**
 * NavigationBarUtils
 *
 * @author Created by lucas on 2023/10/29 16:52
 */
object NavigationBarUtils {

    /**
     * 大部分普通页面的设置
     */
    fun setImmersionBar(
        activity: Activity,
        block: ImmersionBarBuilder.() -> Unit,
    ) {
        ImmersionBar.with(activity)
            .run {
                ImmersionBarBuilder(this).let {
                    it.block()
                    it.build()
                }
            }
            .init()
    }

    /**
     * 个人主页设置
     */
    fun setImmersionBarForUserSpace(activity: Activity) {
        ImmersionBar.with(activity)
            .transparentStatusBar()
            .statusBarDarkFont(true)
            .navigationBarDarkIcon(true)
            .navigationBarColor(R.color.white)
            .init()
    }

    /**
     * 主页tab的设置
     */
    fun setImmersionBarForMain(fragment: Fragment, tabView: View) {
        ImmersionBar.with(fragment)
            .statusBarDarkFont(true)
            .titleBarMarginTop(tabView)
            .navigationBarDarkIcon(true)
            .transparentNavigationBar()
            .init()
    }

    /**
     * 主页live的设置
     */
    fun setImmersionBarForMainLive(fragment: Fragment, tabView: View) {
        ImmersionBar.with(fragment)
            .statusBarDarkFont(false)
            .titleBarMarginTop(tabView)
            .transparentNavigationBar()
            .init()
    }

    /**
     * 如果底部是透明的则设置底部控件的底边距为导航栏高度
     */
    fun setNavigationHeightIfTransparent(view: View, block: ((View, Int) -> Unit)? = null) {
        val window = ViewUtils.getWindow(view.context)
        if (window != null && isTransparentNavigationBar(window)) {
            setNavigationHeight(view, block)
        }
    }

    /**
     * 设置控件的底边距为导航栏高度
     */
    fun setNavigationHeight(view: View, block: ((View, Int) -> Unit)? = null) {
        val height = getNavigationBarHeight(view.context) + 1
        if (block == null) {
            val sourceBottomMargin = view.getTag(R.id.source_bottom_margin_tag)
            val viewBottomMargin = if (sourceBottomMargin is Int) {
                sourceBottomMargin
            } else {
                ViewUtils.getBottomMargin(view).apply {
                    view.setTag(R.id.source_bottom_margin_tag, this)
                }
            }

            ViewUtils.setMargin(
                view,
                bottomMargin = height
            )
        } else {
            block(view, height)
        }
    }

    /**
     * 判断是否为透明的导航栏
     */
    fun isTransparentNavigationBar(activity: Activity): Boolean {
        return isTransparentNavigationBar(activity.window)
    }

    /**
     * 判断是否为透明的导航栏
     */
    fun isTransparentNavigationBar(window: Window): Boolean {
        val color = BarUtils.getNavBarColor(window)
        val isTransparent = color == 0
        if (isTransparent) {
            return true
        }
        if (OSUtils.isVivo() || OSUtils.isFuntouchOrOriginOs()) {
            val alpha = color shr 24 and 0xff

            return alpha != 0xff
        }

        return false
    }

    /**
     * 判断导航栏是否显示
     */
    fun isShowNavigationBar(context: Context): Boolean {
        val id = context.resources.getIdentifier("config_showNavigationBar", "bool", "android")
        return id > 0 && context.resources.getBoolean(id)
    }

    /**
     * 获取导航栏模式，-1代表按钮
     */
    fun getNavigationBarMode(context: Context): Int {
        val resourceId =
            context.resources.getIdentifier("config_navBarInteractionMode", "integer", "android")
        return if (resourceId > 0) {
            context.resources.getInteger(resourceId)
        } else {
            -1
        }
    }

    /**
     * 获取导航栏高度
     */
    fun getNavigationBarHeight(context: Context): Int {
        val hasNavigationBar = if (!isShowNavigationBar(context)) {
            when (context) {
                is Activity -> ImmersionBar.hasNavigationBar(context)
                else -> ImmersionBar.hasNavigationBar(context)
            }
        } else {
            true
        }
        //vivo如果是全面屏也会被判断为有导航栏，所以需要判断是否开启了手势
        if (hasNavigationBar && (OSUtils.isVivo() || OSUtils.isFuntouchOrOriginOs())) {
            val type = Settings.Secure.getInt(context.contentResolver, "navigation_gesture_on", -1)
            if (type == 1 || type == 2) {
                return 4.dp
            }
        }

        val resourceId =
            context.resources.getIdentifier("navigation_bar_height", "dimen", "android")

        return if (resourceId > 0 && hasNavigationBar) {
            context.resources.getDimensionPixelSize(resourceId)
        } else {
            0
        }
    }

}

class ImmersionBarBuilder internal constructor(
    private val immersionBar: ImmersionBar,
) {

    private var titleBar: View? = null
    private var statusBarIsDarkFont: Boolean = true
    private var statusBarColor: Int = R.color.white
    private var navigationBarIsDarkFont: Boolean = true
    private var navigationBarColor: Int = R.color.white
    private var builder: (ImmersionBar.() -> ImmersionBar)? = null

    /**
     * 设置顶部view，用于沉浸式
     */
    fun setTitleBar(view: View): ImmersionBarBuilder {
        this.titleBar = view
        return this
    }

    /**
     * 设置状态栏字体颜色是否为深色，默认为深色
     */
    fun setStatusBarDarkFont(isDarkFont: Boolean): ImmersionBarBuilder {
        this.statusBarIsDarkFont = isDarkFont
        return this
    }

    /**
     * 设置默认状态栏颜色，默认白色
     */
    fun setStatusBarColor(@ColorRes color: Int): ImmersionBarBuilder {
        this.statusBarColor = color
        return this
    }

    /**
     * 设置导航栏字体颜色是否为深色，默认为深色
     */
    fun setNavigationBarDarkFont(isDarkFont: Boolean): ImmersionBarBuilder {
        this.navigationBarIsDarkFont = isDarkFont
        return this
    }

    /**
     * 设置默认导航栏颜色，默认白色
     */
    fun setNavigationBarColor(@ColorRes color: Int): ImmersionBarBuilder {
        this.navigationBarColor = color
        return this
    }

    /**
     * 设置其他沉浸式
     */
    fun attachImmersionBar(builder: ImmersionBar.() -> ImmersionBar): ImmersionBarBuilder {
        this.builder = builder
        return this
    }

    internal fun build(): ImmersionBar {
        if (titleBar != null) {
            immersionBar.titleBarMarginTop(titleBar)
        } else {
            immersionBar.fitsSystemWindows(true)
                .statusBarColor(statusBarColor)
        }

        immersionBar.statusBarDarkFont(statusBarIsDarkFont)
            .navigationBarColor(navigationBarColor)
            .navigationBarDarkIcon(navigationBarIsDarkFont)

        builder?.invoke(immersionBar)

        return immersionBar
    }
}
